package com.example.myapplication;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * Created by xuanyang on 2017/11/18.
 */

public class FloatUtil {


    public static void showView(final Context context){

        WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
        final RelativeLayout relView = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.float_layout, null);
        final WindowManager mWindowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);


        ImageView im = (ImageView) relView.findViewById(R.id.imageView1);

        im.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // test
            }
        });

        relView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                return false;
            }
        });

        relView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        mParams.type = WindowManager.LayoutParams.TYPE_PHONE;

        mParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        mParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        mParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        mParams.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            mParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
        }
        mParams.flags |= WindowManager.LayoutParams.FLAG_SECURE;

        mParams.format = PixelFormat.TRANSLUCENT;
        mParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        mParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        mParams.gravity = Gravity.CENTER;



        mWindowManager.addView(relView, mParams);

//        mWindowManager.removeView(relView);
    }






}
